package com.cs.archivesmanagement.action;

import com.cs.archivesmanagement.dao.EntityDao;
import com.cs.archivesmanagement.entity.AmAccount;
import com.cs.archivesmanagement.entity.AmDepartment;
import com.cs.archivesmanagement.entity.AmRole;
import com.cs.archivesmanagement.service.AccountService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by amo on 2015/8/17.
 */

@Controller
@Namespace("/")
@Results({@Result(name = "loginSuccessUser", location = "/admin_user.html"),
        @Result(name = "loginSuccessManager", location = "/admin_manager.html"),
        @Result(name = "loginSuccessAdmin", location = "/admin_surpermanager.jsp"),
        @Result(name = "loginError", location = "/index.jsp"),
        @Result(name = "registerSuccess", location = "/index.jsp"),
        @Result(name = "registerError", location = "/index.jsp"),
        @Result(name = "getAccountList", location = "/systemManager/user.jsp"),
        @Result(name = "deleteSucceed", location = "/systemManager/user.jsp"),
        @Result(name = "getAccountList", location = "/systemManager/user.jsp"),
        @Result(name = "entermodify", location = "/systemManager/user_alter.jsp"),
        @Result(name = "entermodifysuccess", location = "/systemManager/user.jsp")

})

public class AccountManagementAction extends ActionSupport {


    private String userName;
    private String pwd;
    private String nickname;
    private Integer gender;
    private String contact;
    private Integer accountId;
    private Integer accountRole;
    private Integer accountDepartment;


    private AmAccount amAccount;


    private AmAccount accountBeingModify;

    private String message;

    @Resource
    AccountService accountService;

    @Resource
    EntityDao entityDao;

    private Integer amAccountBemodifyId;


    private ArrayList<AmAccount> amAccountArrayList;


    public List<AmRole> getAmRolesList() {
        return amRolesList;
    }

    public void setAmRolesList(List<AmRole> amRolesList) {
        this.amRolesList = amRolesList;
    }

    private List<AmRole> amRolesList;

    public List<AmDepartment> getAmDepartmentsList() {
        return amDepartmentsList;
    }

    public void setAmDepartmentsList(List<AmDepartment> amDepartmentsList) {
        this.amDepartmentsList = amDepartmentsList;
    }

    private List<AmDepartment> amDepartmentsList;

    @org.apache.struts2.convention.annotation.Action("getAccountList")
    public String getUserInfoAction() {


        amAccountArrayList = accountService.getAllAccount();
        return "getAccountList";


    }


    @org.apache.struts2.convention.annotation.Action("modifyaccount")
    public String modifyAccount() {

        //     System.out.println("准备查询");

        accountBeingModify = accountService.findById(amAccountBemodifyId);
        amRolesList=accountService.getAllAmRole();
        amDepartmentsList=accountService.getAllAmDepartment();
        return "entermodify";

    }

    @org.apache.struts2.convention.annotation.Action("finishmodify")

    public String finishmodify() {

        AmAccount amAccount = new AmAccount();
        AmRole amRole = (AmRole) entityDao.getEntity(AmRole.class, accountRole);

        AmDepartment amDepartment = (AmDepartment) entityDao.getEntity(AmDepartment.class, accountDepartment);

        System.out.println(amDepartment.getDepartmentName());
        System.out.println(amRole.getRoleName());
        amAccount.setAccountAc(userName);
        // System.out.println(amAccount.getAccountAc());
        amAccount.setAccountContact(contact);
        // System.out.println(amAccount.getAccountContact());
        amAccount.setAccountDepartment(amDepartment);
        amAccount.setAccountGender(gender);
        // System.out.println(amAccount.getAccountGender());
        amAccount.setAccountId(accountId);
        //System.out.println(accountId);
        amAccount.setAccountNickname(nickname);
        amAccount.setAccountPsw(pwd);
        amAccount.setAccountRole(amRole);

        //System.out.println(amAccount.getAccountId()+"--"+amAccount.getAccountPsw());
        if (entityDao.updateEntity(amAccount)) {
            System.out.println("succuss");
        }

        amAccountArrayList = accountService.getAllAccount();

        return "entermodifysuccess";
    }


    @org.apache.struts2.convention.annotation.Action("login")
    public String check() {

        System.out.println("用户登陆");
        amAccount = accountService.login(userName, pwd);

        if (amAccount != null) {
            message = "登陆成功";

            return "loginSuccessAdmin";

        } else {
            message = "账户名或者密码错误";
            return "loginError";
        }

    }


    @org.apache.struts2.convention.annotation.Action("deleteaccount")

    public String deleteAccount() {


        accountService.deleteAccount(new AmAccount(amAccountBemodifyId));


        amAccountArrayList = accountService.getAllAccount();
        return "deleteSucceed";


    }


    @org.apache.struts2.convention.annotation.Action("regist")
    public String regist() {

        AmAccount amAccount = new AmAccount();
        amAccount.setAccountContact(contact);
        amAccount.setAccountAc(userName);
        amAccount.setAccountPsw(pwd);
        amAccount.setAccountGender(gender);
        amAccount.setAccountNickname(nickname);

        AmDepartment amDepartment = new AmDepartment();
        amDepartment.setDepartmentId(1);

        AmRole amRole = new AmRole();
        amRole.setRoleId(1);

        amAccount.setAccountDepartment(amDepartment);
        amAccount.setAccountRole(amRole);


        if (accountService.regist(amAccount)) {

            System.out.println("注册成功");
            message = "注册成功，现在可以登录了";
            return "registerSuccess";
        } else {

            System.out.println("注册失败");
            message = "注册失败，用户名重复";
            return "registerError";

        }


    }



    public Integer getAccountDepartment() {
        return accountDepartment;
    }

    public void setAccountDepartment(Integer accountDepartment) {
        this.accountDepartment = accountDepartment;
    }

    public Integer getAccountRole() {

        return accountRole;
    }

    public void setAccountRole(Integer accountRole) {
        this.accountRole = accountRole;
    }

    public Integer getAccountId() {
        return accountId;
    }

    public void setAccountId(Integer accountId) {
        this.accountId = accountId;
    }

    public AmAccount getAccountBeingModify() {
        return accountBeingModify;
    }

    public void setAccountBeingModify(AmAccount accountBeingModify) {
        this.accountBeingModify = accountBeingModify;
    }

    public void setAmAccount(AmAccount amAccount) {
        this.amAccount = amAccount;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public AmAccount getAmAccount() {
        return amAccount;
    }

    public AccountService getAccountService() {
        return accountService;
    }

    public String getMessage() {
        return message;
    }

    public String getPwd() {
        return pwd;
    }

    public String getUserName() {
        return userName;
    }

    public Integer getGender() {
        return gender;
    }

    public String getContact() {
        return contact;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public ArrayList<AmAccount> getAmAccountArrayList() {
        return amAccountArrayList;
    }

    public void setAmAccountArrayList(ArrayList<AmAccount> amAccountArrayList) {
        this.amAccountArrayList = amAccountArrayList;
    }


    public Integer getAmAccountBemodifyId() {
        return amAccountBemodifyId;
    }

    public void setAmAccountBemodifyId(Integer amAccountBemodifyId) {
        this.amAccountBemodifyId = amAccountBemodifyId;
    }
}
